home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / Chip Temmuz 2004.iso / program / antispam / RazorAgent_SDK / razor-agents-sdk-2.03.exe / Time-HiRes-01.20 / t / 01test.t next >
Encoding:
Text File  |  1998-07-02  |  3.3 KB  |  141 lines

  1. # Before `make install' is performed this script should be runnable with
  2. # `make test'. After `make install' it should work as `perl test.pl'
  3.  
  4. ######################### We start with some black magic to print on failure.
  5.  
  6. # Change 1..1 below to 1..last_test_to_print .
  7. # (It may become useful if the test is moved to ./t subdirectory.)
  8.  
  9. BEGIN { $| = 1; print "1..14\n"; }
  10. END {print "not ok 1\n" unless $loaded;}
  11. use Time::HiRes qw(tv_interval);
  12. $loaded = 1;
  13. print "ok 1\n";
  14.  
  15. ######################### End of black magic.
  16.  
  17. # Insert your test code below (better if it prints "ok 13"
  18. # (correspondingly "not ok 13") depending on the success of chunk 13
  19. # of the test code):
  20.  
  21. use strict;
  22.  
  23. my $have_gettimeofday    = defined &Time::HiRes::gettimeofday;
  24. my $have_usleep        = defined &Time::HiRes::usleep;
  25. my $have_ualarm        = defined &Time::HiRes::ualarm;
  26.  
  27. import Time::HiRes 'gettimeofday'    if $have_gettimeofday;
  28. import Time::HiRes 'usleep'        if $have_usleep;
  29. import Time::HiRes 'ualarm'        if $have_ualarm;
  30.  
  31. sub skip {
  32.     map { print "ok $_ (skipped)\n" } @_;
  33. }
  34.  
  35. sub ok {
  36.     my ($n, $result, @info) = @_;
  37.     if ($result) {
  38.         print "ok $n\n";
  39.     }
  40.     else {
  41.     print "not ok $n\n";
  42.         print "# @info\n" if @info;
  43.     }
  44. }
  45.  
  46. if (!$have_gettimeofday) {
  47.     skip 2..6;
  48. }
  49. else {
  50.     my @one = gettimeofday();
  51.     ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
  52.     ok 3, $one[0] > 850_000_000, "@one too small";
  53.  
  54.     sleep 1;
  55.  
  56.     my @two = gettimeofday();
  57.     ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
  58.             "@two is not greater than @one";
  59.  
  60.     my $f = Time::HiRes::time;
  61.     ok 5, $f > 850_000_000, "$f too small";
  62.     ok 6, $f - $two[0] < 2, "$f - @two >= 2";
  63. }
  64.  
  65. if (!$have_usleep) {
  66.     skip 7..8;
  67. }
  68. else {
  69.     my $one = time;
  70.     usleep(10_000);
  71.     my $two = time;
  72.     usleep(10_000);
  73.     my $three = time;
  74.     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
  75.  
  76.     if (!$have_gettimeofday) {
  77.         skip 8;
  78.     }
  79.     else {
  80.         my $f = Time::HiRes::time;
  81.     usleep(500_000);
  82.         my $f2 = Time::HiRes::time;
  83.     my $d = $f2 - $f;
  84.     ok 8, $d > 0.4 && $d < 0.8, "slept $d secs $f to $f2";
  85.     }
  86. }
  87.  
  88. # Two-arg tv_interval() is always available.
  89. {
  90.     my $f = tv_interval [5, 100_000], [10, 500_000];
  91.     ok 9, $f == 5.4, $f;
  92. }
  93.  
  94. if (!$have_gettimeofday) {
  95.     skip 10;
  96. }
  97. else {
  98.     my $r = [gettimeofday()];
  99.     my $f = tv_interval $r;
  100.     ok 10, $f < 2, $f;
  101. }
  102.  
  103. if (!$have_usleep) {
  104.     skip 11;
  105. }
  106. else {
  107.     my $r = [gettimeofday()];
  108.     #jTime::HiRes::sleep 0.5;
  109.     Time::HiRes::sleep( 0.5 );
  110.     my $f = tv_interval $r;
  111.     ok 11, $f > 0.4 && $f < 0.8, "slept $f secs";
  112. }
  113.  
  114. if (!$have_ualarm) {
  115.     skip 12..13;
  116. }
  117. else {
  118.     my $tick = 0;
  119.     local $SIG{ALRM} = sub { $tick++ };
  120.  
  121.     my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
  122.     my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
  123.     my $three = time;
  124.     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
  125.  
  126.     $tick = 0;
  127.     ualarm(10_000, 10_000);
  128.     sleep until $tick >= 3;
  129.     ok 13, 1;
  130.     ualarm(0);
  131. }
  132.  
  133. # new test: did we even get close?
  134.  
  135. {
  136.  my $t = time();
  137.  my $tf = Time::HiRes::time();
  138.  ok 14, ($tf >= $t) && (($tf - $t) <= 1),
  139.   "time $t differs from Time::HiRes::time $tf";
  140. }
  141.